package com.cjkj.equipment.controller;

import com.cjkj.common.core.constant.EquipmentConstants;
import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.domain.runset.RunSetParam;
import com.cjkj.common.core.utils.bean.BeanUtils;
import com.cjkj.common.security.annotation.InnerAuth;
import com.cjkj.equipment.domain.MonitorPadData;
import com.cjkj.equipment.domain.SmartBraceletData;
import com.cjkj.equipment.service.MonitorPadDataService;
import com.cjkj.equipment.service.SmartBraceletDataService;
import com.cjkj.equipment.service.TJ03DataService;
import com.cjkj.system.api.domain.APIMonitorPadData;
import com.cjkj.system.api.domain.APISmartBraceletData;
import com.cjkj.system.api.domain.APITJ03RealTimeData;
import com.cjkj.system.api.domain.request.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备相关接口
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Resource
    private MonitorPadDataService monitorPadDataService;
    @Resource
    private SmartBraceletDataService smartBraceletDataService;
    @Resource
    private TJ03DataService tj03DataService;

    /**
     * 获取一个时间区间的最新几条数据
     * @param req
     * @return
     */
    @InnerAuth
    @PostMapping("/getLatestDatas")
    public R<List<APISmartBraceletData>> getLatestDatas(@RequestBody LatestDatasReq req){

        List<SmartBraceletData> latestDatas = new ArrayList<>();

        List<LatestDatasReqEquipmentInfo> equipmentList = req.getEquipmentList();
        if(equipmentList != null && ! equipmentList.isEmpty()) {
            for(LatestDatasReqEquipmentInfo equipment : equipmentList) {
                req.setEquipmentNo(equipment.getEquipmentNo());
                req.setStartTime(equipment.getCreateTime());
                latestDatas.addAll(smartBraceletDataService.getLatestDatas(req));
            }
        } else {
            latestDatas = smartBraceletDataService.getLatestDatas(req);
        }

        List<APISmartBraceletData> data = new ArrayList<>();
        for (SmartBraceletData smartBraceletData : latestDatas) {
            APISmartBraceletData braceletData = new APISmartBraceletData();
            BeanUtils.copyProperties(smartBraceletData, braceletData);
            data.add(braceletData);
        }

        return R.ok(data);
    }

    /**
     * 睡眠监测垫 查询患者就诊期间，设备的最新一条监测数据
     * @param req 查询参数
     * @return
     */
    @PostMapping("/getLastMonitorPadDataInUse")
    public R<APIMonitorPadData> getLastMonitorPadData(@RequestBody PatientEquipmentRTDReq req){
        MonitorPadData lastData = monitorPadDataService.getLastDataOnUse(req);
        if(lastData == null){
            return R.ok();
        }
        APIMonitorPadData apiMonitorPadData = new APIMonitorPadData();
        BeanUtils.copyProperties(lastData,apiMonitorPadData);
        return R.ok(apiMonitorPadData);
    }

    /**
     * 多参数监测仪 查询患者就诊期间，设备的最新一条监测数据
     * @param req 查询参数
     * @return
     */
    @PostMapping("/getLastBraceletDataInUse")
    public R<APISmartBraceletData> getLastBraceletDataInUse(@RequestBody PatientEquipmentRTDReq req){
        SmartBraceletData lastData = smartBraceletDataService.getLastData(req);
        if(lastData == null){
            return R.ok();
        }
        APISmartBraceletData braceletData = new APISmartBraceletData();
        BeanUtils.copyProperties(lastData,braceletData);
        return R.ok(braceletData);
    }

    @PostMapping("/listEquipmentHDA")
    public R<Object> listEquipmentHDA(@RequestBody EquipmentHDAReq req){
        if(EquipmentConstants.EQUIPMENT_TYPE_MONITOR_PAD_TYPE.equals(req.getEquipmentType())){
            return R.ok(monitorPadDataService.appListData(req));
        }else if(EquipmentConstants.EQUIPMENT_TYPE_SMART_BRACELET_TYPE.equals(req.getEquipmentType())){
            return R.ok(smartBraceletDataService.appListData(req));
        }else {
            return R.fail("设备类型错误");
        }
    }

    /**
     * 请求手环小程序端历史记录数据
     * @param req
     * @return
     */
    @PostMapping("/reqBraceletHistoryData")
    public R<Object> reqBraceletHistoryData(@RequestBody BraceletHistoryDataReq req){
        return R.ok(smartBraceletDataService.getBraceletHistoryData(req));
    }

    /**
     * 向设备发送命令
     */
    @InnerAuth
    @PostMapping("sendCommand/{equipmentNo}")
    public R<Object> sendCommand(@PathVariable("equipmentNo") String equipmentNo, @RequestBody RunSetParam runSet){
        return R.ok(tj03DataService.sendCommand(equipmentNo, runSet));
    }

    /**
     * 获取多参数监测仪最新电量
     */
    @InnerAuth
    @GetMapping("getTJ03LatestBatteryData")
    public R<APITJ03RealTimeData> getTJ03LatestBatteryData(String equipmentNo) {
        return R.ok(tj03DataService.getLatestBatteryData(equipmentNo));
    }

    /**
     * 获取多参数监测仪最新数据
     */
    @InnerAuth
    @GetMapping("getTJ03LatestData")
    public R<APITJ03RealTimeData> getTJ03LatestData(String equipmentNo) {
        return R.ok(tj03DataService.getLatestData(equipmentNo));
    }
}
